#pragma once
#include <utils/Mutex.h>
#include <deque>

namespace android {

template<typename T>
class LockedDeque {
public:
    LockedDeque() {}
    ~LockedDeque() {}

    const T& front() {
        Mutex::Autolock lock(m);
        return q.front();
    }

    void pop_front() {
        Mutex::Autolock lock(m);
        q.pop_front();
    }

    void push_back(const T& value) {
        Mutex::Autolock lock(m);
        q.push_back(value);
    }

    bool empty() {
        Mutex::Autolock lock(m);
        return q.empty();
    }

    void clear() {
        Mutex::Autolock lock(m);
        q.clear();
    }

    void free() {
        Mutex::Autolock lock(m);
        for (auto *buffer : q) {
            delete buffer;
        }
        q.clear();
    }

    size_t size() {
        return q.size();
    }

private:
    std::deque<T> q;
    mutable Mutex m;
};

} //namespace
